//
//  RequestTool.m
//  BeautifulLockerRoom
//
//  Created by mac on 16/7/28.
//  Copyright © 2016年 Winny. All rights reserved.
//

#import "RequestTool.h"

@implementation RequestTool

+(void)getNetworkRequestWithUrl:(NSString *)url date:(Block)block
{
    
    AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager GET:url parameters:nil success:^(NSURLSessionDataTask *task, id responseObject)
    {
        NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        if (block)
        {
            block(dic);
        }
    }
         failure:^(NSURLSessionDataTask *task, NSError *error)
    {
        NSLog(@"%@",error);
    }];
    
}

+(NSString *)getOtherDataURLWithValue:(NSString *)value
{
    NSString * str = [NSString stringWithFormat:@"http://api.yuike.com/gmall/api/1.0/allbuy/list.php?category_ids=0&count=40&cursor=0&mid=457465&sid=4a1cc94e0cac3750572ef022fd111548&sort=default&type=%@&yk_appid=1&yk_cbv=2.9.5&yk_pid=1&yk_user_id=5198416",value];
    return str;
}

+(void)getMainDataURLWithdate:(Block)block
{
    [self getNetworkRequestWithUrl:@"http://api.yuike.com/gmall/api/1.0/allbuy/list.php?category_ids=4796%2C4797%2C4805%2C4838%2C4839%2C4840%2C4841%2C4843&count=40&cursor=0&mid=457465&sid=4a1cc94e0cac3750572ef022fd111548&sort=default&type=dress&yk_appid=1&yk_cbv=2.9.5&yk_pid=1&yk_user_id=5198416" date:^(NSDictionary *dic) {
        if (block) {
            block(dic);
        }
        
    }];
}

+(void)getAllModelWithValue:(NSString *)value data:(ModelBlock)block
{
        NSString * url = [self getOtherDataURLWithValue:value];
        NSMutableArray * modelArray = [[NSMutableArray alloc]init];
        [self getNetworkRequestWithUrl:url date:^(NSDictionary *dic)
        {
            NSDictionary * data = [dic objectForKey:@"data"];
            NSArray * products = [data objectForKey:@"products"];
            for (int j = 0; j < products.count; j ++)
            {
                NSDictionary * modelData = products[j];
                DataModel * model = [DataModel mj_objectWithKeyValues:modelData];
                [modelArray addObject:model];
            }
            block(modelArray);
        }];
}


#pragma mark - 推荐以外上部分数据请求URL
+(void)getOtherAllModelWithValue:(NSString *)value data:(ModelBlock)block
{
    NSString * url = [self getOtherOtherDataURLWithValue:value];
    NSMutableArray * modelArray = [[NSMutableArray alloc]init];
    [self getNetworkRequestWithUrl:url date:^(NSDictionary *dic)
     {
         NSDictionary * data = [dic objectForKey:@"data"];
         NSArray * category_list = [data objectForKey:@"category_list"];
         for (int j = 0; j < category_list.count; j ++)
         {
             NSDictionary * modelData = category_list[j];
             DataModel * model = [DataModel mj_objectWithKeyValues:modelData];
             [modelArray addObject:model];
         }
         block(modelArray);
     }];
}

+(NSString *)getOtherOtherDataURLWithValue:(NSString *)value
{
    NSString * str = [NSString stringWithFormat:@"http://api.yuike.com/gmall/api/1.0/allbuy/category_list.php?mid=457465&sid=4a1cc94e0cac3750572ef022fd111548&type=%@&yk_appid=1&yk_cbv=2.9.5&yk_pid=1&yk_user_id=5198416",value];
    return str;
}

#pragma mark - 精选界面请求数据URL
//获取大的字典
+(void)getPersonalModelWithData:(Block)block
{
    [self getNetworkRequestWithUrl:PERSONAL_URL date:^(NSDictionary *dic) {
        block(dic);
    }];
}
//解析所需要的数据
+(void)getPersonalDataWithBlock:(ModelBlock)block
{
    
    [self getPersonalModelWithData:^(NSDictionary *dic) {
        NSDictionary * data = [dic objectForKey:@"data"];
        NSArray * products  = [data objectForKey:@"products"];
        NSMutableArray * array = [NSMutableArray array];
        for (int i = 0; i < products.count; i ++)
        {
            NSDictionary * item = products[i];
            DataModel * model = [DataModel mj_objectWithKeyValues:item];
            [array addObject:model];
        }
        block(array);
    }];
}

@end
